/**
 * 自定义查询组件
 * 依赖meeting.core.js
 */
(function (mt) {

    mt.customsearch = function (code, option) {
        var defaultOption = {
            title: "",
            type: "list",
            multiple: false,
            index: 0,
            width: 800,
            height: 500,
            param: {},
            callback: {
                default: true,
                preload: empty,
                loading: empty,
                loaded: empty,
                close: empty,
                cancel: empty,
                result: empty
            }
        }

        defaultOption = $.extend(true, defaultOption, option);

        /*===============================封装方法-begin=======================================*/

        //获取弹窗内容
        function getcontent() {
            var self = this;
            return $.ajax({
                type: "POST",
                url: proName + "/customsearch/dialog",
                contentType: "application/json",
                data: JSON.stringify({
                    type: defaultOption.type,
                    multiple: defaultOption.multiple,
                    code: code,
                    defaultParam: defaultOption.param,
                    index: defaultOption.index
                })
            }).done(function (response) {
                self.setContent(response);
            })
        }

        //默认处理返回结果函数
        function defaultResult(result) {

            var selectresult = {};
            if (defaultOption.multiple) {
                for (var i = 0; i < result.length; i++) {
                    if (i == 0) {
                        for (attr in result[i]) {
                            selectresult[attr] = result[i][attr];
                        }
                    } else {
                        for (attr in result[i]) {
                            selectresult[attr] += "," + result[i][attr];
                        }
                    }
                }

            } else {
                selectresult = result;
            }

            $(":input[sysSelectResultName]").each(function (a, b) {
                var name = $(b).attr("sysSelectResultName");
                if (defaultOption.index != "0") {
                    var len = name.indexOf("_" + defaultOption.index);
                    if (len > 0) {
                        $(b).val(selectresult[name.substring(0, len)]);
                    }
                } else {
                    $(b).val(selectresult[name]);
                }
            })

            return selectresult;
        }

        /*===============================封装方法-end=======================================*/

        var confirmOption = {
            title: defaultOption.title,
            boxWidth: defaultOption.width,
            useBootstrap: false,
            content: getcontent,
            onOpenBefore: function () {
                defaultOption.callback.preload();
            },
            contentLoaded: function (data, status, xhr) {
                defaultOption.callback.loading(data, status, xhr);
            },
            onContentReady: function () {
                this.$content.find(".contentidHid" + defaultOption.index).val(this.$content.attr("id"));
                defaultOption.callback.loaded();
            },
            onClose: function () {
                defaultOption.callback.close();
            },
            buttons: {
                save: {
                    text: "确认",
                    isHidden: defaultOption.multiple ? false : true,
                    btnClass: 'btn-blue',
                    action: function () {
                        this.$content.find(".getresultBtn" + defaultOption.index).click();
                        var result = JSON.parse(this.$content.find(".resultHid" + defaultOption.index).val());
                        var selectresult;
                        if (defaultOption.callback.default) {
                            selectresult = defaultResult(result);
                        }

                        if (defaultOption.callback.default) {
                            defaultOption.callback.result(result, selectresult);
                        } else {
                            defaultOption.callback.result(result);
                        }
                    }
                },
                cancel: {
                    text: "取消",
                    action: function () {
                        defaultOption.callback.cancel();
                    }
                }
            }
        }

        //打开弹窗
        $.confirm(confirmOption)
    }

    //不执行任何动作方法
    function empty() {

    }
})(meeting);